package com.masterclient.app.businessmodule.vipmodule.card;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.View;

import com.eoner.baselib.presenter.FCBasePresenter;
import com.eoner.baselib.presenter.IFCBaseView;
import com.eoner.baselib.utils.other.TUtil;
import com.eoner.baselib.widget.empty.FCPageStateView;
import com.kaopiz.kprogresshud.KProgressHUD;

import butterknife.ButterKnife;

/**
 * 带请求的卡片样式
 * @param <T>
 * @param <T1>
 */
public abstract class FCBaseCard<T extends FCBasePresenter,T1> extends IBaseCard<T1> implements IFCBaseView {
    protected Context mContext;
    protected T mPresenter;
    //commitLoading
    private KProgressHUD mCommitLoading;

    public FCBaseCard(View itemView, Context context) {
        super(itemView,context);
        ButterKnife.bind(this,itemView);
        this.mContext = context;
        mPresenter = TUtil.getT(this,0);
        if (mPresenter != null) {
            mPresenter.setIView(this);
        }
    }

    public abstract void onBindViewHolder(T1 t, int i);

    @Override
    public Context context() {
        return mContext;
    }

    /**
     * 显示页面指示器
     * */
    @Override
    public void baseShowPageLoading(boolean isCanTouchOutSide) {
    }

    /**
     * 隐藏页面指示器
     * */
    @Override
    public void baseHiddenPageLoading() {
    }

    /**
     * 显示提交指示器
     * */
    @Override
    public void baseShowCommitLoading(String msg) {
        if (mCommitLoading == null) {
            mCommitLoading = KProgressHUD.create(mContext)
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE);
//            mCommitLoading.setSize(114,92);
            mCommitLoading.setBackgroundColor(Color.parseColor("#80000000"));
            mCommitLoading.setCancellable(false);
            if (msg != null && msg.length() > 0) {
                mCommitLoading.setLabel(msg);
            }
            if(mContext != null){
                Activity activity = (Activity) mContext;
                if (activity != null && !activity.isFinishing() && !mCommitLoading.isShowing()) {
                    mCommitLoading.show();
                }
            }

        } else if (!mCommitLoading.isShowing() ) {
            if(mContext != null){
                Activity activity = (Activity) mContext;
                if (activity != null && !activity.isFinishing() && ! activity.hasWindowFocus()) {
                    mCommitLoading.show();
                }
            }
        }
    }

    /**
     * 隐藏提交指示器
     * */
    @Override
    public void baseHiddenCommitLoading() {
        if (mCommitLoading != null) {
            try {
                if(mContext != null){
                    Activity activity = (Activity) mContext;
                    if (activity != null && !activity.isFinishing()) {
                        mCommitLoading.dismiss();
                    }
                }
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        }
        mCommitLoading = null;
    }

    @Override
    public FCPageStateView baseRefreshPageState(int viewState) {
        return null;
    }

    @Override
    public FCPageStateView baseRefreshPageState(int viewState, String msg) {
        return null;
    }

    @Override
    public FCPageStateView baseRefreshPageState(int viewState, int img, String msg) {
        return null;
    }

    @Override
    public FCPageStateView baseRefreshPageStateTopOffset(int topOffset) {
        return null;
    }

    @Override
    public FCPageStateView baseRefreshPageStateBottomOffset(int bottomOffset) {
        return null;
    }

    @Override
    public void baseShowError() {

    }

    @Override
    public void baseShowError(String identifer) {

    }
}
